package swu.cx.myglidetest

import java.util.concurrent.LinkedBlockingQueue

class RequestManager private constructor(){
    val requestQueue:LinkedBlockingQueue<BitmapRequest> = LinkedBlockingQueue()
    fun addBitmapRequest(request: BitmapRequest){
        if (!requestQueue.contains(request)){
            requestQueue.add(request)
        }

    }
    init {
        createAndStartDispatchers()
    }
    companion object{
        var instance:RequestManager?=null
        @JvmName("getInstance1")
        fun getInstance() = instance?: synchronized(this){
            instance?:RequestManager().also { instance = it }
        }
    }
    private lateinit var dispatchers:Array<BitmapDispatcher?>
    private fun createAndStartDispatchers(){
        val threadCount = Runtime.getRuntime().availableProcessors()
        dispatchers = arrayOfNulls(threadCount)
        for (i in dispatchers.indices){
            val bitmapDispatcher = BitmapDispatcher(requestQueue)
            bitmapDispatcher.start()
            dispatchers[i] = bitmapDispatcher
        }
    }
}